﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QLTB.Data;
using QLTB.Entities;
using _4rumDCT.App_Code;

namespace QuanLyThietBi.DCTEDU_.QuanTri
{
    public partial class QLLienHe : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Pager1.CurrentIndex = 1;
                BindLienHe(Pager1.CurrentIndex);
            }
        }

        private void BindLienHe(int PageNo)
        {
            string WhereClause = "";
            int TotalRows = 0;
            if (chkTrangThai.Checked == true)
            {
                WhereClause += " TrangThai=0";
            }
            DataSet dsLienHe = DataRepository.Provider.ExecuteDataSet("LienHe_GetPaged", WhereClause, "NgayCapNhat DESC",
                PageNo - 1, Pager1.PageSize);
            rpLienHe.DataSource = dsLienHe.Tables[0];
            rpLienHe.DataBind();
            try
            {
                TotalRows = Convert.ToInt32(dsLienHe.Tables[1].Rows[0][0]);
                litCountLienHe.Text = " (Tổng cộng có: <font color=red><b>" + TotalRows.ToString() + "</b></font> liên hệ)";
            }
            catch (Exception)
            {
            }
            Pager1.ItemCount = TotalRows;
            if (TotalRows <= Pager1.PageSize)
                Pager1.Visible = false;
            else
                Pager1.Visible = true;
        }

        protected void Pager1_Command(object sender, CommandEventArgs e)
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            BindLienHe(currnetPageIndex);
        }
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["netTiersConnectionString"].ConnectionString);
        protected void cmdSend_OnClick(object sender, EventArgs e)
        {
            bool status = false;
            GiaiDap ent_GiaiDap = new GiaiDap();
            ent_GiaiDap.MaLienHe = Convert.ToInt32(H_LienHeId.Value);
            ent_GiaiDap.NoiDung = txtPhanHoi.Text;
            ent_GiaiDap.NgayCapNhat = Common.GetServerDateTime();
            string query = string.Empty;

            conn.Open();
            query = "UPDATE LienHe SET TrangThai = 1 where MaLienHe = " + H_LienHeId.Value;
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = conn;
            sqlCommand.CommandText = query;
            sqlCommand.ExecuteNonQuery();
            conn.Close();
            GiaiDap ent_GiaiDap2 = DataRepository.GiaiDapProvider.GetByMaLienHe(Convert.ToInt32(H_LienHeId.Value));
            if (ent_GiaiDap2 != null)
            {
                ent_GiaiDap.MaCanBo = ent_GiaiDap2.MaCanBo;
                status = DataRepository.GiaiDapProvider.Update(ent_GiaiDap);
            }
            else
            {
                status = DataRepository.GiaiDapProvider.Insert(ent_GiaiDap);
            }
            if (!status)
            {
                ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THATBAI, Constant.CAPNHAT_THATBAI, true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(upPanel1, this.GetType(), Constant.THANHCONG, Constant.CAPNHAT_THANHCONG, true);
                txtNoiDung.Text = "";
                txtPhanHoi.Text = "";
                txtTieuDe.Text = "";
                BindLienHe(Pager1.CurrentIndex);
            }
        }

        protected void chkTrangThai_OnCheckedChanged(object sender, EventArgs e)
        {
            BindLienHe(Pager1.CurrentIndex);
        }

        protected void rpLienHe_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                H_LienHeId.Value = id.ToString();
                LienHe ent_LienHe = DataRepository.LienHeProvider.GetByMaLienHe(id);
                txtTieuDe.Text = ent_LienHe.TieuDe;
                txtNoiDung.Text = ent_LienHe.NoiDung;
                GiaiDap entGiaiDap = DataRepository.GiaiDapProvider.GetByMaLienHe(id);
                if (entGiaiDap != null)
                {
                    txtPhanHoi.Text = entGiaiDap.NoiDung;
                }
                pnThem.Visible = true;
            }
            if (e.CommandName == "Delete")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                DataRepository.GiaiDapProvider.Delete(id);
            }
            BindLienHe(Pager1.CurrentIndex);
        }

        public string getTenCanBo(object ob)
        {
            string id = ob.ToString();
            return DataRepository.CanBoProvider.GetByMaCanBo(id).HoTen;

        }

        public string getPhanHoi(object ob)
        {
            string id = ob.ToString();
            GiaiDap ent_GiaiDap = DataRepository.GiaiDapProvider.GetByMaLienHe(Convert.ToInt32(id));
            if (ent_GiaiDap!=null)
            return ent_GiaiDap.NoiDung;
            else
            {
                return "";
            }

        }
    }
}